import { Controller, Get, Post, Body, Patch, Param, Delete, Query } from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { JwtService } from '@nestjs/jwt';
import * as SvgCaptcha from 'svg-captcha'
import { RedisServer } from 'src/config/Redis';
import { PublicRoute } from 'src/decorations/PublicRoute';
import { UserService } from './user.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import Utils from 'src/utils/Utils';
import Constant from 'src/config/Constant';

@ApiTags('用户')
@Controller('user')
export class UserController {
  constructor(
    private readonly userService: UserService,
    private readonly jwtService: JwtService,
    private readonly cacheManager: RedisServer,
  ) { }

  @ApiOperation({ summary: '验证码' })
  @PublicRoute()
  @Get('code')
  getCode() {
    const captcha = SvgCaptcha.create({
      width: 120,
      height: 55,
      size: 4, // 字符数
      charPreset: "0123456789qwertyuiopasdfghjklzxcvbnm", // 过滤字符
      noise: 4, // 干扰线条数
      color: true,
      background: "#F5F5F5", // 背景颜色
    });
    captcha.data = captcha.data.replace(/^data:image\/svg\+xml;base64,/, '');
    return captcha
  }

  @ApiOperation({ summary: '用户注册' })
  @PublicRoute()
  @Post('register')
  register(@Body() createUserDto: CreateUserDto) {
    const { userName, mobile, password, rePassword, code, uuid } = createUserDto
    if (!userName) throw '用户名不能为空~'
    if (!mobile) throw '手机号不能为空~'
    if (!Utils.isPhone(mobile)) throw '手机号格式不正确~'
    if (!password || !rePassword) throw '密码不能为空~'
    if (!code) throw '请输入验证码~'
    if (code != uuid) throw '请输入正确的验证码~'
    return this.userService.create(createUserDto);
  }

  @ApiOperation({ summary: '用户登录' })
  @PublicRoute()
  @Post('login')
  async Login(@Body() createUserDto: CreateUserDto) {
    const { mobile, password, code, uuid } = createUserDto;
    if (code != uuid) throw '请输入正确的验证码~'
    const user = await this.userService.findOneByMobile(mobile);
    if (!user) throw '未查询到账号信息~'
    if (!user.status) throw '当前注册账号未审核通过~'
    const md5 = Utils.MD5(password, user.salt);
    if (md5 !== user.password) throw '登录密码错误~'
    const info: any = {
      guid: user.guid,
      mobile: user.mobile,
      name: user.userName,
      role: user.role,
      avatar: user.avatar,
    }
    const token = await this.jwtService.signAsync(info);
    info.token = token
    const redis = await this.cacheManager.getClient();
    redis.SETEX(`${Utils.redisTokenKey(user.guid)}`, Constant.token_expiry_time, token);

    return info
  }

  @ApiOperation({ summary: 'guid查询用户' })
  @Get('find/guid')
  findUserByGuid(@Query('guid') guid: string) {
    return this.userService.findOneByGuid(guid)
  }

  @ApiOperation({ summary: '手机号查询用户' })
  @Get('find/mobile')
  findUserByMobile(@Query('mobile') mobile: string) {
    return this.userService.findOneByMobile(mobile)
  }

  @ApiOperation({ summary: '用户信息更新' })
  @Post('update/userInfo')
  async updateUserInfo(@Body() updateUserDto: UpdateUserDto) {
    return this.userService.update(updateUserDto)
  }
}
